<template xmlns:el-col="http://www.w3.org/1999/html">
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
      <el-row :gutter="10">
        <el-col :span="6">
      <el-form-item label="比赛" prop="race">
        <el-input
          v-model="queryParams.race"
          placeholder="请输入比赛"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
        </el-col>
        <el-col :span="6">
      <el-form-item label="项目名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
        </el-col>
      </el-row>


      <el-row :gutter="10">
        <el-col :span="6">
      <el-form-item label="队长" prop="leader">
        <el-input
          v-model="queryParams.leader"
          placeholder="请输入队长"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
        </el-col>
        <el-col :span="6">
      <el-form-item label="招募要求" prop="tLimit">
        <el-input
          v-model="queryParams.tLimit"
          placeholder="请输入招募要求"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
        </el-col>
        <el-col :span="6">
      <el-form-item label="招募人数" prop="capacity">
        <el-input
          v-model="queryParams.capacity"
          placeholder="请输入招募人数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="10">
        <el-col :span="6">
      <el-form-item label="招聘开始时间" prop="start"style="width: 295px">
        <el-date-picker clearable
          v-model="queryParams.start"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择招聘开始时间">
        </el-date-picker>
      </el-form-item>
        </el-col>
        <el-col :span="6">
      <el-form-item label="招聘结束时间" prop="end" style="width: 295px">
        <el-date-picker clearable
          v-model="queryParams.end"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择招聘结束时间">
        </el-date-picker>
      </el-form-item>
        </el-col>
      </el-row>

      <el-row :gutter="10">
        <el-col :span="6">
      <el-form-item label="材料审核状态" prop="status" style="width: 295px">
        <el-select v-model="queryParams.status" placeholder="请选择材料审核状态" clearable>
          <el-option
            v-for="dict in review_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
        </el-col>
        <el-col :span="6">
      <el-form-item label="不合格原因" prop="reason">
        <el-input
          v-model="queryParams.reason"
          placeholder="请输入不合格原因"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
        </el-col>
      </el-row>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['buis:teamInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['buis:teamInfo:edit']"
        >修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['buis:teamInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['buis:teamInfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="teamInfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="比赛" align="center" prop="race" />
      <el-table-column label="项目名称" align="center" prop="name" />
      <el-table-column label="项目简介" align="center" prop="description" />
      <el-table-column label="队长" align="center" prop="leader" />
      <el-table-column label="招募要求" align="center" prop="tLimit" />
      <el-table-column label="招募人数" align="center" prop="capacity" />
      <el-table-column label="招聘开始时间" align="center" prop="start" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.start, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="招聘结束时间" align="center" prop="end" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.end, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="材料审核状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="review_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="不合格原因" align="center" prop="reason" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['buis:teamInfo:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['buis:teamInfo:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改队伍信息对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="teamInfoRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="比赛" prop="race">
          <el-input v-model="form.race" placeholder="请输入比赛" />
        </el-form-item>
        <el-form-item label="项目名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入项目名称" />
        </el-form-item>
        <el-form-item label="项目简介" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="队长" prop="leader">
          <el-input v-model="form.leader" placeholder="请输入队长" />
        </el-form-item>
        <el-form-item label="招募要求" prop="tLimit">
          <el-input v-model="form.tLimit" placeholder="请输入招募要求" />
        </el-form-item>
        <el-form-item label="招募人数" prop="capacity">
          <el-input v-model="form.capacity" placeholder="请输入招募人数" />
        </el-form-item>
        <el-form-item label="招聘开始时间" prop="start">
          <el-date-picker clearable
            v-model="form.start"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择招聘开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="招聘结束时间" prop="end">
          <el-date-picker clearable
            v-model="form.end"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择招聘结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="奖项" prop="rRank">
          <el-input v-model="form.rRank" placeholder="请输入奖项" />
        </el-form-item>
        <el-form-item label="材料审核状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in review_status"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="不合格原因" prop="reason">
          <el-input v-model="form.reason" placeholder="请输入不合格原因" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="TeamInfo">
import { listTeamInfo, getTeamInfo, delTeamInfo, addTeamInfo, updateTeamInfo } from "@/api/buis/teamInfo";

const { proxy } = getCurrentInstance();
const { review_status } = proxy.useDict('review_status');

const teamInfoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    race: null,
    name: null,
    description: null,
    leader: null,
    tLimit: null,
    capacity: null,
    start: null,
    end: null,
    status: null,
    reason: null,
  },
  rules: {
    race: [
      { required: true, message: "比赛不能为空", trigger: "blur" }
    ],
    name: [
      { required: true, message: "项目名称不能为空", trigger: "blur" }
    ],
    description: [
      { required: true, message: "项目简介不能为空", trigger: "blur" }
    ],
    leader: [
      { required: true, message: "队长不能为空", trigger: "blur" }
    ],
    tLimit: [
      { required: true, message: "招募要求不能为空", trigger: "blur" }
    ],
    capacity: [
      { required: true, message: "招募人数不能为空", trigger: "blur" }
    ],
    start: [
      { required: true, message: "招聘开始时间不能为空", trigger: "blur" }
    ],
    end: [
      { required: true, message: "招聘结束时间不能为空", trigger: "blur" }
    ],
    status: [
      { required: true, message: "材料审核状态不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询队伍信息列表 */
function getList() {
  loading.value = true;
  listTeamInfo(queryParams.value).then(response => {
    teamInfoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    race: null,
    name: null,
    description: null,
    leader: null,
    tLimit: null,
    capacity: null,
    start: null,
    end: null,
    rRank: null,
    status: null,
    reason: null,
    isDeleted: null,
    createTime: null,
    updateTime: null
  };
  proxy.resetForm("teamInfoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加队伍信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getTeamInfo(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改队伍信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["teamInfoRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateTeamInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addTeamInfo(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除队伍信息编号为"' + _ids + '"的数据项？').then(function() {
    return delTeamInfo(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('buis/teamInfo/export', {
    ...queryParams.value
  }, `teamInfo_${new Date().getTime()}.xlsx`)
}

getList();
</script>
